package com.coldchain.cys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.context.CarDriverContext;
import com.coldchain.cys.entity.CarDriver;
import com.coldchain.cys.entity.DriverFeedback;
import com.coldchain.cys.mapper.DriverFeedbackMapper;
import com.coldchain.cys.page.DriverFeedbackQueryVo;
import com.coldchain.cys.service.ICarDriverService;
import com.coldchain.cys.service.IDriverFeedbackService;
import com.fangliai.core.context.BaseContextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianpeng
 * @since 2018-09-20
 */
@Service
public class DriverFeedbackServiceImpl extends ServiceImpl<DriverFeedbackMapper, DriverFeedback> implements IDriverFeedbackService {

	@Autowired
	private ICarDriverService carDriverService ;
	
	@Transactional
	@Override
	public BaseResponse doAddDriverFeedback(DriverFeedback driverFeedback) {
		String driverid = BaseContextHandler.getUserID();
		CarDriver driver = carDriverService.getById(driverid);
		if(driver == null) {
			return getErrorMessage("请先登录") ;
		}
		if(StrUtil.isBlank(driverFeedback.getContent())) {
			return getErrorMessage("请填写反馈的内容") ;
		}
		driverFeedback.setDriverid(driverid);
		boolean result = super.save(driverFeedback);
		if(!result) {
			return getErrorMessage("操作失败") ;
		}
		return new ObjectRestResponse<>().succeed("反馈成功") ;
	}
	
	private BaseResponse getErrorMessage(String message) {
		return new ObjectRestResponse<>().error(message)  ;
	}

	@Override
	public void getList(DriverFeedbackQueryVo vo) {
		QueryWrapper<DriverFeedback> ew = new QueryWrapper<DriverFeedback>();
		ew.orderByDesc("createtime") ;
		if(vo.getDriverid() != null) {
			ew.eq("driverid", vo.getDriverid()) ;
		}
		super.page(vo, ew) ;
	}
}
